/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUI.java
 *
 * Created on 21.09.2009, 15:55:45
 */
package tictactoe;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Rudi
 */
public class GUI extends javax.swing.JFrame implements ActionListener
{
    private final static ImageIcon ROT = new ImageIcon("tictactoe/rss-red-32.png");
    private final static ImageIcon GRUEN = new ImageIcon("tictactoe/rss-green-32.png");

    private JButton[][] buttons;
    private Spielfeld spielfeld;

    /** Creates new form GUI */
    public GUI(Spielfeld spielfeld)
    {
        this.spielfeld = spielfeld;
        initComponents();

        feld.setLayout(new GridLayout(spielfeld.getNZeilen(), spielfeld.getNSpalten()));
        buttons = new JButton[spielfeld.getNZeilen()][spielfeld.getNSpalten()];
        for (int z = 0; z < buttons.length; z++)
        {
            for (int s = 0; s < buttons[0].length; s++)
            {
                buttons[z][s] = new JButton();
                feld.add(buttons[z][s]);
                buttons[z][s].addActionListener(this);
            }
        }
        update();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        feld = new javax.swing.JPanel();
        status = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        spiel = new javax.swing.JMenu();
        neuesSpiel = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tictactoe");

        javax.swing.GroupLayout feldLayout = new javax.swing.GroupLayout(feld);
        feld.setLayout(feldLayout);
        feldLayout.setHorizontalGroup(
            feldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        feldLayout.setVerticalGroup(
            feldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 279, Short.MAX_VALUE)
        );

        getContentPane().add(feld, java.awt.BorderLayout.CENTER);
        getContentPane().add(status, java.awt.BorderLayout.SOUTH);

        spiel.setText("Spiel");

        neuesSpiel.setText("neu");
        neuesSpiel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                neuesSpiel(evt);
            }
        });
        spiel.add(neuesSpiel);

        jMenuBar1.add(spiel);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void neuesSpiel(java.awt.event.ActionEvent evt)//GEN-FIRST:event_neuesSpiel
    {//GEN-HEADEREND:event_neuesSpiel
        // TODO add your handling code here:
    }//GEN-LAST:event_neuesSpiel
    /**
     * @param args the command line arguments
     */
    /*
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new GUI().setVisible(true);
    }
    });

    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel feld;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem neuesSpiel;
    private javax.swing.JMenu spiel;
    private javax.swing.JLabel status;
    // End of variables declaration//GEN-END:variables

    public void actionPerformed(ActionEvent e)
    {
        for (int z = 0; z < buttons.length; z++)
        {
            for (int s = 0; s < buttons[0].length; s++)
            {
                if (buttons[z][s] == e.getSource())
                {
                    try
                    {
                        spielfeld.set(z, s);
                    } catch (Exception ex)
                    {
                        JOptionPane.showMessageDialog(this, ex.getMessage());
                    }
                }
            }
        }
        update();
    }

    private void update()
    {
        for (int z = 0; z < buttons.length; z++)
        {
            for (int s = 0; s < buttons[0].length; s++)
            {
                switch(spielfeld.get(z, s))
                {
                    case GRUEN:
                        buttons[z][s].setIcon(GRUEN);
                        break;

                    case ROT:
                        buttons[z][s].setIcon(ROT);
                        break;

                    case LEER:
                        buttons[z][s].setIcon(null);
                        break;
                }

            }
        }
        switch(spielfeld.getSieger())
        {
            case GRUEN:
                status.setText("Grün hat gewonnen");
                break;

            case ROT:
                status.setText("Rot hat gewonnen");
                break;

            case LEER:
                switch(spielfeld.getNext())
                {
                    case GRUEN:
                        status.setText("Grün kommt dran");
                        break;

                    case ROT:
                        status.setText("Rot kommt dran");
                        break;
                }
                break;
        }
    }
}
